// 初始化设置
let settings = {
    monthlySalary: 0,
    workDays: 22,
    workStart: '09:00',
    workEnd: '18:00',
    hasLunchBreak: false,
    lunchStart: '12:00',
    lunchEnd: '13:00'
};

// 从存储中加载设置
chrome.storage.sync.get('workSettings', (data) => {
    if (data.workSettings) {
        settings = data.workSettings;
        updateSettingsUI();
    }
});

// 更新设置UI
function updateSettingsUI() {
    document.getElementById('monthly-salary').value = settings.monthlySalary;
    document.getElementById('work-days').value = settings.workDays;
    document.getElementById('work-start').value = settings.workStart;
    document.getElementById('work-end').value = settings.workEnd;
    document.getElementById('has-lunch-break').checked = settings.hasLunchBreak;
    document.getElementById('lunch-start').value = settings.lunchStart;
    document.getElementById('lunch-end').value = settings.lunchEnd;
    document.getElementById('lunch-time-group').style.display = settings.hasLunchBreak ? 'block' : 'none';
}

// 保存设置
document.getElementById('save-settings').addEventListener('click', () => {
    settings = {
        monthlySalary: parseFloat(document.getElementById('monthly-salary').value) || 0,
        workDays: parseInt(document.getElementById('work-days').value) || 22,
        workStart: document.getElementById('work-start').value,
        workEnd: document.getElementById('work-end').value,
        hasLunchBreak: document.getElementById('has-lunch-break').checked,
        lunchStart: document.getElementById('lunch-start').value,
        lunchEnd: document.getElementById('lunch-end').value
    };
    chrome.storage.sync.set({ workSettings: settings }, () => {
        updateWorkProgress();
    });
});

// 显示/隐藏午休时间设置
document.getElementById('has-lunch-break').addEventListener('change', (e) => {
    document.getElementById('lunch-time-group').style.display = e.target.checked ? 'block' : 'none';
});

// 更新时间和日期
function updateDateTime() {
    const now = new Date();
    const timeString = now.toLocaleTimeString('zh-CN', { hour: '2-digit', minute: '2-digit', second: '2-digit' });
    const dateString = now.toLocaleDateString('zh-CN', { 
        year: 'numeric', 
        month: 'long', 
        day: 'numeric',
        weekday: 'long'
    });
    
    document.getElementById('current-time').textContent = timeString;
    document.getElementById('current-date').textContent = dateString;
}

// 计算距离周末的时间
function updateWeekendCountdown() {
    const now = new Date();
    const daysUntilWeekend = 5 - now.getDay();
    const hoursUntilWeekend = 24 - now.getHours();
    const minutesUntilWeekend = 60 - now.getMinutes();
    
    let countdownText = '';
    if (daysUntilWeekend > 0) {
        countdownText = `${daysUntilWeekend}天${hoursUntilWeekend}小时${minutesUntilWeekend}分钟`;
    } else {
        countdownText = '周末啦！';
    }
    
    document.getElementById('weekend-countdown').textContent = countdownText;
}

// 计算工作进度和收入
function updateWorkProgress() {
    const now = new Date();
    const currentTime = now.getHours() * 60 + now.getMinutes();
    
    // 转换工作时间
    const [workStartHour, workStartMin] = settings.workStart.split(':').map(Number);
    const [workEndHour, workEndMin] = settings.workEnd.split(':').map(Number);
    const workStartMinutes = workStartHour * 60 + workStartMin;
    const workEndMinutes = workEndHour * 60 + workEndMin;
    
    // 计算总工作时间（分钟）
    let totalWorkMinutes = workEndMinutes - workStartMinutes;
    
    // 如果有午休，减去午休时间
    if (settings.hasLunchBreak) {
        const [lunchStartHour, lunchStartMin] = settings.lunchStart.split(':').map(Number);
        const [lunchEndHour, lunchEndMin] = settings.lunchEnd.split(':').map(Number);
        const lunchStartMinutes = lunchStartHour * 60 + lunchStartMin;
        const lunchEndMinutes = lunchEndHour * 60 + lunchEndMin;
        totalWorkMinutes -= (lunchEndMinutes - lunchStartMinutes);
    }
    
    // 计算当前工作进度
    let workedMinutes = 0;
    if (currentTime >= workStartMinutes) {
        if (currentTime <= workEndMinutes) {
            workedMinutes = currentTime - workStartMinutes;
            // 如果有午休且在午休时间内，减去午休时间
            if (settings.hasLunchBreak) {
                const [lunchStartHour, lunchStartMin] = settings.lunchStart.split(':').map(Number);
                const [lunchEndHour, lunchEndMin] = settings.lunchEnd.split(':').map(Number);
                const lunchStartMinutes = lunchStartHour * 60 + lunchStartMin;
                const lunchEndMinutes = lunchEndHour * 60 + lunchEndMin;
                if (currentTime > lunchEndMinutes) {
                    workedMinutes -= (lunchEndMinutes - lunchStartMinutes);
                } else if (currentTime > lunchStartMinutes) {
                    workedMinutes -= (currentTime - lunchStartMinutes);
                }
            }
        } else {
            workedMinutes = totalWorkMinutes;
        }
    }
    
    // 计算进度百分比
    const progress = Math.min(100, Math.max(0, (workedMinutes / totalWorkMinutes) * 100));
    document.getElementById('work-progress').style.width = `${progress}%`;
    document.getElementById('progress-text').textContent = `已完成 ${progress.toFixed(1)}%`;
    
    // 计算时薪和今日收入
    const hourlyRate = settings.monthlySalary / (settings.workDays * (totalWorkMinutes / 60));
    const todayEarnings = (workedMinutes / 60) * hourlyRate;
    
    document.getElementById('hourly-rate').textContent = `¥${hourlyRate.toFixed(2)}`;
    document.getElementById('today-earnings').textContent = `¥${todayEarnings.toFixed(2)}`;
}

// 每分钟更新一次
setInterval(() => {
    updateDateTime();
    updateWeekendCountdown();
    updateWorkProgress();
}, 60000);

// 初始化显示
updateDateTime();
updateWeekendCountdown();
updateWorkProgress(); 